//带快递费的总额
var totalFee = 0;
//不带快递费的总额
var fee = 0;
var productId = getUrlParam('productId');
var num = getUrlParam('num');
var normalId = getUrlParam('normalId');
var addressId = 0;
var memo;
var serviceId;
var isSince;
var carIds = getUrlParam('carIds');
addressId=0;
$(function () {
    //productId=421&num=1&normalId=522&memberId=623


    console.log(carIds);
    getConfirmDetailcart(carIds);

    $('#btnJiesuan').click(function () {
        var carts = ""
        var $cart = $('.cart-item .checkbox.on');


        if ($cart.length == 0) {
            alertMsg("请选择");
            return;
        }
        $cart.each(function () {
            var $this = $(this);
            //  console.log(this);
            var cid = $this.attr("cid");
            carts += cid + ",";
        });
        console.log(carts);
        console.log(carts.split(',').length);
        var s = carts.slice(0, carts.length - 1);
        console.log(s);
        window.location.href = 'order1.html?carIds=' + s;
    })


});

function toPeisong() {
    window.location.href="/service-add.html?addressId="+addressId;
}


function getConfirmDetailcart(carIds) {

    $.ajax({
        type: "post",
        url: ajaxUrl + "/mobile/order/carToOrderConfirmOrder",
        async: true,
        data: {'memberId': memberId, 'carIds': carIds},
        success: function (data) {
            console.log(data.result.productDetails.length)
            if (data.result.productDetails.length==0){
               window.location.href="/cart1.html";
            }
            if (data.code != 0) {
     //           alert(data.message);
                window.history.go(-1);
                window.history.go(0)
            }
            
            console.log(data);
            var result = data.result;
            if (result.receiveAddress == null) {
                $('#addressTitle').text("新增地址");
            } else {
                addressId=result.receiveAddress.id;
                setAddress(result.receiveAddress);
            }

            var productDetails = result.productDetails;
            setProductDetails(productDetails);
            $('#canGetScore').text(result.canGetScore + "积分");

            $('#proFee').text(result.totalFee + '元');

            $('#totalFee').text(result.totalFee + result.serviceFee);
            totalFee = result.totalFee + result.serviceFee;
            fee = result.totalFee;
            $('#serviceFee').text(result.serviceFee + '元');
            $('#totalNum').text(result.totalNum);

            var fwdHtml = '';
            fwdHtml += '<div class="addr">' + result.servicePointDto.name + '   ' + result.servicePointDto.mobile + '</div>';
            fwdHtml += '<div class="addr">' + result.servicePointDto.address + '</div>';
            $('#fwd').html(fwdHtml);
            serviceId = result.servicePointDto.id;
        }

    });
}

//设置详情
function setProductDetails(prodcutDetails) {
    var html = '';
    for (var i = 0; i < prodcutDetails.length; i++) {
        html += '<a href="javascript:;" class="order-item bottom-border">';
        html += '<img src="' + prodcutDetails[i].productImage + '" alt="" class="img">';
        html += '<div class="text">';
        html += '<div class="name">' + prodcutDetails[i].productName + '</div>';
        html += '';
        html += '</div>';
        html += '<div class="price-num">';
        html += '<div class="price"><i>￥</i>' + prodcutDetails[i].price + '</div>';
        html += '<div class="num">X ' + prodcutDetails[i].num + '</div>';
        html += '</div>'
    }
    $('#pList').html(html);
}

function setAddress(address) {
    $('#uname').html(address.name + '<i class="tel">' + address.mobile + '</i>');
    $('#addressName').text(address.provinceName +
        address.cityName + address.districtName + address.streetName + address.address);
    addressId = address.id;

}

